package teletutor.classroommanager.UI;

import teletutor.classroommanager.services.MemberProxy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JToggleButton;
import teletutor.classroommanager.services.MemberStateObserver;
import teletutor.core.services.SimpleMessage;

/**
 * The control that serves as a thumbnail for the member, and also provides 
 * functions of controlling audio transmission and sending PM
 * @author Sabin Timalsena
 * @deprecated Use the MemberThumb component instead
 */
public class MemberThumbPanel extends javax.swing.JPanel implements MemberStateObserver {
    // TODO the controls and the methods to handle CURIOSITY aka the MemberProxy.STUDENT_CURIOUS state
    // TODO the picture in the thumbnail, if not available in database, use from local store
    
    MemberProxy mProxy;

    /** Creates new form MemberThumbPanel */
    public MemberThumbPanel(MemberProxy mProxy) {
        initComponents();

        this.mProxy = mProxy;
        mProxy.addMemberStateObserver(this);

        // modify component values and wire them up with the component
        nameLabel.setText(mProxy.getUsername());
        // TODO get the image from the proxy
        // imagePanel.setImage etc

        // if this is not on the tutor node, or this proxy represents the 
        // tutor node, disable the audio button
        if (!mProxy.isOnTutorNode() || mProxy.isTutor()) {
            audioButton.setEnabled(false);
        }
        
        JToggleButton tb = new JToggleButton();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        audioButton = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        pmButton = new javax.swing.JButton();
        imagePanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        stateText = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setMaximumSize(new java.awt.Dimension(145, 207));
        setMinimumSize(new java.awt.Dimension(120, 120));

        audioButton.setText("A");
        audioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                audioButtonActionPerformed(evt);
            }
        });

        jToggleButton2.setText("jToggleButton2");

        pmButton.setText("P");
        pmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pmButtonActionPerformed(evt);
            }
        });

        imagePanel.setBackground(new java.awt.Color(102, 102, 0));
        imagePanel.setBorder(new javax.swing.border.MatteBorder(null));

        javax.swing.GroupLayout imagePanelLayout = new javax.swing.GroupLayout(imagePanel);
        imagePanel.setLayout(imagePanelLayout);
        imagePanelLayout.setHorizontalGroup(
            imagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 62, Short.MAX_VALUE)
        );
        imagePanelLayout.setVerticalGroup(
            imagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 68, Short.MAX_VALUE)
        );

        nameLabel.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        nameLabel.setText("Member Name");

        stateText.setForeground(new java.awt.Color(0, 0, 153));
        stateText.setText("Status");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(imagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(pmButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(audioButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(26, 26, 26))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                        .addGap(26, 26, 26))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(stateText)
                        .addContainerGap(91, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(audioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(pmButton))
                    .addComponent(imagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(nameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stateText)
                .addGap(41, 41, 41)
                .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void audioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_audioButtonActionPerformed
        /* send a message to the AudioManager of the member and tell it to
         * pause/resume transmission. At the same time, change the state of this
         * member to STUDENT_ACTIVE/STUDENT_PASSIVE (toggle behavior)
         */
        if (audioButton.isSelected()) {
            if (mProxy.getState() == MemberProxy.STUDENT_PASSIVE 
                    || mProxy.getState() == MemberProxy.STUDENT_CURIOUS) {
                mProxy.setState(MemberProxy.STUDENT_ACTIVE);
                
                try {
                    // send a message to AudioManager
                    mProxy.sendObject(mProxy.getUsername(), "AudioManager", new SimpleMessage("resume"));
                } catch (Exception ex) {
                    Logger.getLogger(MemberThumbPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        // if button unselected, pause the audio
        else {
            if (mProxy.getState() == MemberProxy.STUDENT_ACTIVE) {
                mProxy.setState(MemberProxy.STUDENT_PASSIVE);
                try {
                    // send a message to AudioManager
                    mProxy.sendObject(mProxy.getUsername(), "AudioManager", new SimpleMessage("pause"));
                } catch (Exception ex) {
                    Logger.getLogger(MemberThumbPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_audioButtonActionPerformed

    private void pmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pmButtonActionPerformed
        mProxy.showPMFrame();
    }//GEN-LAST:event_pmButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton audioButton;
    private javax.swing.JPanel imagePanel;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton pmButton;
    private javax.swing.JLabel stateText;
    // End of variables declaration//GEN-END:variables

    /**
     * This method will be called as soon as this Panel is registered as a 
     * member state observer
     * @param memberState 
     */
    @Override
    public void memberStateChanged(int memberState) {
        // TODO change appearance based on member state changes
        switch (memberState) {
            case MemberProxy.STUDENT_ACTIVE:
                stateText.setText("Active Student");
                break;
            case MemberProxy.STUDENT_PASSIVE:
                stateText.setText("Passive Student");
                break;
            case MemberProxy.STUDENT_CURIOUS:
                 stateText.setText("Curious");
                break;
            case MemberProxy.TUTOR:
                 stateText.setText("Tutor");
                break;
            case MemberProxy.SUSPECT:
                // TODO disable the controls on this event, re-enable later
                break;

        }
    }
}
